package org.lq.er.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.er.dao.AdminDao;
import org.lq.er.entity.Admin;
import org.lq.er.entity.Student;
import org.lq.er.util.Instantiation;
import org.lq.er.util.JDBCUtil;
/**
 * 管理员数据连接实现类
 * @author 邹倩倩
 *
 * @package org.lq.er.dao.impl
 */
public class AdminDaoImpl implements AdminDao,Instantiation<Admin>{
	/**
	 * 添加管理员
	 */
	@Override
	public int save(Admin t) {
		String sql = "insert into admin(name,password,phone_no,cardid,status) values(?,?,?,?,?)";
		int flag = 0;
		try {
			flag = JDBCUtil.executeUpdate(sql, t.getName(),t.getPassword(),t.getPhone_no(),t.getCardId(),t.getStatus());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 修改管理员
	 */
	@Override
	public int update(Admin t) {
		String sql = "update admin set name=?,cardid=?,password=?,phone_no=? where id=?";
		int flag = 0;
		try {
			flag = JDBCUtil.executeUpdate(sql,t.getName(),t.getCardId(),t.getPassword(),t.getPhone_no(),t.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 通过编号删除管理员
	 */
	@Override
	public int delete(int id) {
		String sql = "delete from admin where id=?";
		int flag = 0;
		try {
			flag = JDBCUtil.executeUpdate(sql, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public List<Admin> findAll() {
		List<Admin> list = new ArrayList<>();
		String sql = "select * from admin";
		try {
			list = JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 根据编号号查询
	 */
	@Override
	public Admin getById(int id) {
		List<Admin> list = new ArrayList<>();
		Admin admin = null;
		String sql = "select * from admin where id=?";
		try {
			list = JDBCUtil.executeQuery(sql, this,id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		admin = list.get(0);
		return admin;
	}
	@Override
	public Admin instance(ResultSet rs) {
		Admin admin = new Admin();
		try {
			admin.setId(rs.getInt("id"));
			admin.setName(rs.getString("name"));
			admin.setPassword(rs.getString("password"));
			admin.setPhone_no(rs.getString("phone_no"));
			admin.setCardId(rs.getString("cardId"));
			admin.setStatus(rs.getInt("status"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return admin;
	}
	/**
	 * 校验身份
	 */
	@Override
	public Admin check(String cardId, String passwd) {
		// 查询结果
		List<Admin> list = new ArrayList<Admin>();
		// 创建学生实体类
		Admin admin = null;
		// 查询	SQL语句
		String sql = "select * from admin where cardId=? and password = ?";
		try {
			list = JDBCUtil.executeQuery(sql, this,cardId,passwd);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (!list.isEmpty()) {			
			admin = list.get(0);
		} 
		return admin;
	}
	@Override
	public Admin getByCarId(String cardId) {
		List<Admin> list = new ArrayList<>();
		Admin admin = null;
		String sql = "select * from admin where cardId=?";
		try {
			list = JDBCUtil.executeQuery(sql, this,cardId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		admin = list.get(0);
		return admin;
	}

}
